package com.oocl.admin_platform.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.admin_platform.pojo.FailReason;
import com.oocl.admin_platform.pojo.Merchant;
import com.oocl.admin_platform.service.FailReasonService;
import com.oocl.admin_platform.service.MerchantService;
import com.oocl.admin_platform.service.factory.ServiceFactory;
import com.oocl.admin_platform.vo.MerchantVO;

/**
 * Servlet implementation class GetUncheckedDetails
 */
public class GetMerchantDetails extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private MerchantService merchantService;
    private FailReasonService failReasonService;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetMerchantDetails() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void init() throws ServletException {
    	merchantService = ServiceFactory.getMerchantServiceInstance();
    	failReasonService=ServiceFactory.getFailReasonServiceInstance();
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String status = request.getParameter("status");
		Merchant merchant = merchantService.findById(id);
		FailReason failReason=null;
		String imageTypeString=(String) request.getServletContext().getAttribute("imageType");
		String idCardSrc=(String) request.getServletContext().getAttribute("idCardSrc")+"/"+merchant.getUname()+"1."+imageTypeString;
		String portraitSrc=(String) request.getServletContext().getAttribute("portraitSrc")+"/"+merchant.getUname()+"2."+imageTypeString;
		File file = new File("D:\\test\\"+portraitSrc);
		MerchantVO merchantVO=new MerchantVO(id, merchant.getUname(), idCardSrc, portraitSrc, merchant.getAddress(), Integer.parseInt(status), "",file.lastModified());
//		System.out.println("文件id:" +file.lastModified() +"文件名 "+ portraitSrc);
		if ("3".equals(status)) {
			failReason=failReasonService.findByMerchantId(id);
			merchantVO.setReasonContent(failReason.getReasonContent());
		}
		
		request.setAttribute("merchantVO", merchantVO);
		if ("0".equals(status)||"3".equals(status)) {
			request.getRequestDispatcher("uncheckedDetails.jsp").forward(request, response);
		}
		else{
			request.getRequestDispatcher("checkedDetails.jsp").forward(request, response);
		}
		
	}

}
